<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <script>
    /* 
      https://leetcode-cn.com/problems/binary-tree-level-order-traversal/
      思路：https://leetcode-cn.com/problems/binary-tree-level-order-traversal/solution/102er-cha-shu-de-ceng-ci-bian-li-javascript-ti-jie/
      https://leetcode-cn.com/problems/binary-tree-level-order-traversal/solution/yan-du-you-xian-sou-suo-by-shetia/
      
      输入示例：
          3
         / \
        9  20
          /  \
         15   7
      输出示例：
        [
          [3],
          [9,20],
          [15,7]
        ]
     */
    function levelOrder(root) {
      if (!root) return [];
      let res = [];
      let queue = [root];
      let level = 0

      while (queue.length) {
        res[level] = []
        let levelNum = queue.length;
        while (levelNum--) {
          let first = queue.shift();
          res[level].push(first.val);
          first.left && queue.push(first.left);
          first.right && queue.push(first.right);
        }
        level++
      }

      return res;
    }
  </script>
</body>
</html>